package cn.wolfcode.crm.stream;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author caorongqin
 * @date 2020/4/22 20:42
 */
public class Stream {

    @Test
    public void test(){
        List<User> users = new ArrayList<User>();
        User u1 = new User("小卖部","11");
        User u2 = new User("产品部","22");
        User u3 = new User("产品部","33");
        User u4 = new User("信息部","22");
        users.add(u1);
        users.add(u2);
        users.add(u3);
        users.add(u4);
        Set<String> collect = users.stream().map(u -> u.getAge()).collect(Collectors.toSet());
        List<String> list = new ArrayList<>(collect);
        System.err.println(list);
        List<Map<String,String>> ageList = new ArrayList<>();
        List<Map<String,String>> names = new ArrayList<>();
        Map<String,String> ageMap = new HashMap<>();
        Map<String,String> nameMap = new HashMap<>();
        for (User user : users) {
            ageMap.put(user.getAge(),user.getAge());
            nameMap.put(user.getDeptName(),user.getDeptName());
        }

        System.out.println(ageMap);
        System.out.println(nameMap);

    }


    @Test
    public void testDate() throws Exception {
        String d1Start = "2020-04-05 06:30:59";
        String d1End = "2020-04-10 06:30:59";
        String d2Start = "2020-04-07 06:30:59";
        String d2End = "2020-04-15 06:30:59";
        boolean b = Stream.isOverlap(d1Start, d1End, d2Start, d2End);
        System.out.println(b);
    }

    public static boolean isOverlap(String date1Begin, String date1End, String date2Begin, String date2End)throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date leftStartDate = sdf.parse(date1Begin);
        Date leftEndDate = sdf.parse(date1End);
        Date rightStartDate = sdf.parse(date2Begin);
        Date rightEndDate = sdf.parse(date2End);
        return

        ((leftStartDate.getTime() >= rightStartDate.getTime())
                && leftStartDate.getTime() < rightEndDate.getTime())
                ||
                ((leftStartDate.getTime() > rightStartDate.getTime())
                        && leftStartDate.getTime() <= rightEndDate.getTime())
                ||
                ((rightStartDate.getTime() >= leftStartDate.getTime())
                        && rightStartDate.getTime() < leftEndDate.getTime())
                ||
                ((rightStartDate.getTime() > leftStartDate.getTime())
                        && rightStartDate.getTime() <= leftEndDate.getTime());
    }
}

class User{
    String deptName;

    String age;

    public User(String deptName,String age){
        this.age=age;
        this.deptName = deptName;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}
